package com.yangxk.kadmin.web.module.gen.controller;

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.file.FileReportProvider;
import com.yangxk.kadmin.common.beans.Result;
import com.yangxk.kadmin.common.exception.BizException;
import com.yangxk.kadmin.common.exception.ParamException;
import com.yangxk.kadmin.generator.field.Field;
import com.yangxk.kadmin.generator.rules.FormElement;
import com.yangxk.kadmin.generator.rules.Match;
import com.yangxk.kadmin.generator.rules.ValidType;
import com.yangxk.kadmin.generator.template.TemplateType;
import com.yangxk.kadmin.web.common.annotation.DemoModel;
import com.yangxk.kadmin.web.module.gen.entity.GenInfo;
import com.yangxk.kadmin.web.module.gen.entity.GenReportInfo;
import com.yangxk.kadmin.web.module.gen.service.GenService;
import com.yangxk.kadmin.web.module.gen.vo.ShowTable;
import com.yangxk.kadmin.web.module.system.po.Dict;
import com.yangxk.kadmin.web.module.system.service.DictService;
import com.yangxk.ureport.provider.sql.SqlProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/gen")
public class GeneratorController {
    @Autowired
    private GenService genService;
    @Autowired
    private DictService dictService;

    @Autowired(required = false)
    private SqlProvider sqlProvider;
    @Autowired(required = false)
    private FileReportProvider fileReportProvider;

    @RequestMapping("/tables")
    public Result<List<ShowTable>> getTables() throws Exception {
        return Result.ofSuccess(genService.getTables());
    }

    @RequestMapping("/columns")
    public Result<List<Field>> getColumns(String tableName) throws Exception {
        return Result.ofSuccess(genService.getColumns(tableName));
    }

    @RequestMapping("/types")
    public Result<Map<String, Object>> getTypes() {
        Map<String, Object> map = new HashMap<>();
        map.put("valid", ValidType.values()); // 验证模式
        map.put("match", Match.lists()); // 匹配模式
        map.put("formElement", FormElement.values()); // 控件类型
        map.put("templateType", TemplateType.values()); // 模板类型
        map.put("dict", dictService.queryList(DictService.DICT_TYPE_PARENTID)); // 字典类型 TODO
        return Result.ofSuccess(map);
    }

    @DemoModel
    @PostMapping("/gen")
    public Result generator(@RequestBody GenInfo genInfo) {
        genService.generator(genInfo);
        return Result.ofSuccess();
    }

    /**
     * 获取报表文件列表
     * create by yangxk on 2019/2/22 16:09
     */
    @GetMapping("/gen/report/file")
    public Result reportFile() {
        List<ReportFile> res = new ArrayList<>();
        if (sqlProvider != null) {
            res.addAll(sqlProvider.getReportFiles());
        }

        if (fileReportProvider != null) {
            res.addAll(fileReportProvider.getReportFiles());
        }

        return Result.ofSuccess(res);
    }
    /**
     * 代码生成--报表
     * create by yangxk on 2019/2/22 16:01
     */
    @DemoModel
    @PostMapping("/gen/report")
    public Result generator(@RequestBody GenReportInfo reportInfo) {
        genService.generateReport(reportInfo);
        return Result.ofSuccess();
    }
}
